home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / hobbspr2 / sprite.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  3.1 KB  |  146 lines

  1. // Hobbes Mode X Sprite Library
  2.  
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <alloc.h>
  6. #include <stdlib.h>
  7. #include "hobbes.h"
  8. #include "sprite.h"
  9.  
  10.  
  11. /*
  12. Maybe I should use iostream.h, but it's always seemed a bit sloppy to me.
  13. Load in a bitmap from the file named *filnam, and calls
  14. CreateAlignedMaskedImage() to allocate and copy it to screen memory.
  15. */
  16. void    TBitmap::Load_Bitmap(char *fname) {
  17.  
  18.     FILE     *fptr;
  19.     char    dummy[81];
  20.     int        i,j;
  21.  
  22.     if ((fptr = fopen(fname,"r")) == NULL) {
  23.         ::RestoreTextMode();
  24.         printf("Can't open Bitmap file %s",fname);
  25.         exit(1);
  26.         }
  27.  
  28.     fscanf(fptr,"%d",&Width);
  29.     fgets(dummy,80,fptr);
  30.     fscanf(fptr,"%d",&Height);
  31.     fgets(dummy,80,fptr);
  32.  
  33.     if ((Pixels = (char*) malloc (sizeof(char) * Width * Height)) == NULL)
  34.         exit(2);
  35.     if ((Mask = (char*) malloc (sizeof(char) * Width * Height)) == NULL)
  36.         exit(2);
  37.  
  38.     fgets(dummy,80,fptr);
  39.     for (i=0; i<Width; i++) {
  40.         for (j=0; j<Height; j++) {
  41.             fscanf(fptr,"%d",&(Pixels[i*Width + j]));
  42.             }
  43.         fgets(dummy,80,fptr);
  44.         }
  45.  
  46.     fgets(dummy,80,fptr);
  47.     for (i=0; i<Width; i++) {
  48.         for (j=0; j<Height; j++) {
  49.             fscanf(fptr,"%d",&(Mask[i*Width + j]));
  50.             }
  51.         fgets(dummy,80,fptr);
  52.         }
  53.  
  54.  
  55. //    if (CreateAlignedMaskedImage(&Image,
  56. //                Pixels,
  57. //                Width, Height,
  58. //                Mask) == 0) {
  59. //            RestoreTextMode();
  60. //            printf("Couldn't get memory\n");
  61. //            exit(1);
  62. //          }
  63.  
  64. //void CreateAlignedMaskedImage(MaskedImage *ImageToSet, char *Image,
  65. //        int ImageWidth,    int ImageHeight, char *Mask);
  66.  
  67.     ::CreateAlignedMaskedImage(&Image,
  68.                 Pixels,
  69.                 Width, Height,
  70.                 Mask);
  71.     fclose(fptr);
  72. }
  73.  
  74.  
  75. void     TBitmap::Draw(int posX, int posY) {
  76.     CopyScreenToScreenMasked(
  77.         0, 0, Width, Height,
  78.         posX, posY,
  79.         &Image,
  80.         Draw_Offset,
  81.         Virtual_Width_Addr);
  82. }
  83.  
  84.  
  85. BOOL    TBitmap::DrawCkHit(int posX, int posY) {
  86.     BOOL hit=FALSE;
  87.  
  88.     for(int i=posX;(i<posX+Width)&&(!hit);i++)
  89.         for(int j=posY;(j<posY+Height)&&(!hit);j++) {
  90.             hit = (::ReadPixel(i,j) != BLACK);
  91.             }
  92.  
  93.     if (hit)
  94.         return TRUE;
  95.       else {
  96.         ::CopyScreenToScreenMasked(
  97.             0, 0, Width, Height,
  98.             posX, posY,
  99.             &Image,
  100.             Draw_Offset,
  101.             Virtual_Width_Addr);
  102.             return FALSE;
  103.         }
  104. }
  105.  
  106.  
  107. void    TSprite::Load_Sprites(char **fnames, int _numbitmaps) {
  108.  
  109.     NumBitmaps = _numbitmaps;
  110.     Bitmaps = (TBitmap**) malloc (sizeof(TBitmap*) * NumBitmaps);
  111.  
  112.     for (int i=0; i<NumBitmaps; i++)
  113.         Bitmaps[i] = new TBitmap(fnames[i]);
  114. }
  115.  
  116. void TSprite::Draw(void) {
  117.     Update_Position();
  118.     Bitmaps[0]->Draw(GetX(), GetY());
  119. }
  120.  
  121. void TSprite::Draw(int bnum) {
  122.     Update_Position();
  123.     Bitmaps[bnum]->Draw(GetX(), GetY());
  124. }
  125.  
  126. void TSprite::Draw(int bnum, int posX, int posY) {
  127.     Update_Position();
  128.     Bitmaps[bnum]->Draw(posX, posY);
  129. }
  130.  
  131. BOOL TSprite::DrawCkHit(int bnum, int posX, int posY) {
  132.     Update_Position();
  133.     return Bitmaps[bnum]->DrawCkHit(posX, posY);
  134. }
  135.  
  136. // assumes you want a black background
  137. void TSprite::Erase(void) {
  138.     int width  = Bitmaps[0]->GetWidth(),
  139.         height = Bitmaps[0]->GetHeight();
  140.     RectangleFill(GetX(), GetY(), GetX() + width, GetY() + height,BLACK);
  141. }
  142.  
  143.  
  144. //*****************************************************************************
  145. // End of sprite.cpp
  146.